namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class links
    {
        // Methods
        public static void Add(link model)
        {
            StringBuilder builder = new StringBuilder();
            model.sortid = GetMax("sortid", "links") + 1;
            builder.Append("insert into links(");
            builder.Append("[type],[name],[logo],[url],[visible],[sortid]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.type + "',");
            builder.Append("'" + model.name + "',");
            builder.Append("'" + model.logo + "',");
            builder.Append("'" + model.url + "',");
            builder.Append("" + (model.visible ? 1 : 0) + ",");
            builder.Append("" + model.sortid + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete links ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from links where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from links where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int)scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from links ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by sortid ");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from links where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from links where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static int GetMax(string field, string table)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select max([" + field + "]) from [" + table + "]");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 1;
            }
            return int.Parse(scalar.ToString());
        }

        public static link GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from links ");
            builder.Append(" where id=" + id);
            link link = new link();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            link.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            link.type = dataTable.Rows[0]["type"].ToString();
            link.name = dataTable.Rows[0]["name"].ToString();
            link.logo = dataTable.Rows[0]["logo"].ToString();
            link.url = dataTable.Rows[0]["url"].ToString();
            if (dataTable.Rows[0]["visible"].ToString() != "")
            {
                if ((dataTable.Rows[0]["visible"].ToString() == "1") || (dataTable.Rows[0]["visible"].ToString().ToLower() == "true"))
                {
                    link.visible = true;
                }
                else
                {
                    link.visible = false;
                }
            }
            if (dataTable.Rows[0]["sortid"].ToString() != "")
            {
                link.sortid = int.Parse(dataTable.Rows[0]["sortid"].ToString());
            }
            return link;
        }

        public static void Update(link model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update links set ");
            builder.Append("type='" + model.type + "',");
            builder.Append("name='" + model.name + "',");
            builder.Append("logo='" + model.logo + "',");
            builder.Append("url='" + model.url + "',");
            builder.Append("visible=" + (model.visible ? 1 : 0) + ",");
            builder.Append("sortid=" + model.sortid + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }


}

